
clear
version 13
set more off 
// global paths 
global datainpath "/ssb/stamme04/ekstern/apa/wk24/leveranse/"
global dataoutpath "/ssb/stamme04/ekstern/wia/wk48/"
global graphpath "/ssb/stamme04/ekstern/apa/prog/apa/" 
global outputpath "/ssb/stamme04/ekstern/apa/prog/apa/" 

cap log close
log  using  .../AA_6_exp_04Sep2020.smcl, replace


use .../left_cens_spell_10sample_10never_type_flat_2000_1_Sep2020.dta,clear 
capture drop __000*


set more off

foreach y of num 2000/2000 {

        
 	  tempvar gross gross1 alm0 alm1 xss xkom  xsta name b b1 z dedu k
           gen  `gross' = 0
           gen `gross1' = 0
           gen   `ordinary0' = 0
           gen   `ordinary1' = 0
           gen    `sic' = 0
           gen   `flat' = 0
           gen   `surtax' = 0
	   gen   `z' = pred_wage_good
           gen   `b'= 0
           gen   `b1'= `b'*1
	   gen   `dedu' = 0
	   gen `k'=capital_income
	replace `dedu' = min(36600, max((`z'+`b')*.21,30600)) if year==`y'
			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital          
	replace `ordinary1' = max(`ordinary0' - 27700 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>22200) , 0.25*[`gross1'-22200]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-166500,0)*.08   if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	gen  tax_pred_wage_good_`y'_flat = `sic' + `flat' +`surtax'   if year==`y'


	  
	  replace  `gross' = 0
          replace `gross1' = 0
          replace   `ordinary0' = 0
          replace   `ordinary1' = 0
          replace    `sic' = 0
          replace   `flat' = 0
          replace   `surtax' = 0
	  replace   `z' = 0
          replace   `b'= pred_naer
          replace   `b1'= `b'*1
	  replace   `dedu' = 0
	*-* Deductions 
			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital    
	replace `ordinary1' = max(`ordinary0' - 27700 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>22200) , 0.25*[`gross1'-22200]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-166500,0)*.08   if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	gen  tax_pred_naer_`y'_flat = `sic' + `flat' +`surtax'    if year==`y'


	  
      
    
} 

gen tax_wage_flat=. 
gen tax_nae_flat=.
foreach y of num 2000/2000 {
  replace tax_wage_flat = tax_pred_wage_good_`y'_flat if year==`y' 
  replace tax_nae_flat = tax_pred_naer_`y'_flat if year==`y' 
  drop tax_pred_wage_good_`y'_flat
  drop tax_pred_naer_`y'_flat
  }
  

gen netwage_flat= pred_wage_good-tax_wage_flat
replace netwage_flat= pred_wage_good-tax_wage_flat+capital_income if (capital_income>0)
gen netnae_flat= pred_naer-tax_nae_flat
replace netnae_flat= pred_naer-tax_nae_flat +capital_income if (capital_income>0)
 
gen netincdiff_flat=ln((netnae_flat)/(netwage_flat)) 
cap drop __000*  
save  .../left_cens_spell_10sample_10never_type_flat_2000_1_rev1_Sep2020.dta, replace


log close
